import Message from "element-ui/packages/message/src/main";
import * as relationAPI from "../../api/relationAPI";
import Notification from "element-ui/packages/notification/src/main";
import global from "../global";

const relationStorage = {
    state: {
        new_edge_flag: false,
    },
    mutations: {
        set_new_link_flag: function (state, flag) {
            if (flag === state.new_edge_flag) {
                return;
            }
            if (flag) {
                let notice = Notification({
                    ...global.noticeConfig,
                    message: "点击图谱任意两点生成关系"
                });
                global.noticeList.push(notice);
            } else {
                global.noticeList.pop().close();
            }
            state.new_edge_flag = flag;
        }
    },
    actions: {
        getGraphRelationsById: async ({commit}, graphId) => {
            let res = await relationAPI.getGraphRelationsByIdAPI(graphId);
            if (!res) {
                console.log("图关系获取失败");
            }
            return res;
        },
        getGraphNodesById: async ({commit}, nodeId) => {
            let res = await relationAPI.getGraphNodesByIdAPI(nodeId);
            if (!res) {
                console.log("节点关系获取失败");
            }
            return res;
        },
        getRelationById: async ({commit}, relationId) => {
            let res = await relationAPI.getRelationByIdAPI(relationId);
            if (!res) {
                console.log("关系获取失败");
            }
            return res;
        },
        addRelation: async ({commit}, params) => {
            let res = await relationAPI.addRelationAPI(params);
            console.log("params")
            console.log(params)
            if (!res) {
                Message({
                    message: "关系添加失败",
                    type: "error"
                });
            }
            return res;
        },
        deleteRelationById: async ({commit}, relationId) => {
            let res = await relationAPI.deleteRelationByIdAPI(relationId);
            if (!res) {
                Message({
                    message: "关系删除失败",
                    type: "error"
                });
            }
        },
        updateRelation: async ({commit}, relation) => {
            let res = await relationAPI.updateRelationAPI(relation);
            if (!res) {
                Message({
                    message: "关系更新失败",
                    type: "error"
                });
            }
        },
    }
};

export default relationStorage;